import socket
from bdtime import tt
from utils import coding_type

server_addr = ('127.0.0.1', 8888)
# server_addr = ('39.99.42.82', 80)

# server_addr = ('47.97.159.133', 8011)
# client_addr = ('', 8020)

def main():
    tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # tcp_client_socket.bind(client_addr)

    tcp_client_socket.connect(server_addr)

    # send_data = '哈哈哈111'
    # tcp_client_socket.send(send_data.encode(coding_type))

    while 1:
        # msg = input('---- please input:\n')
        msg = f"hello --- {tt.now(1)}"
        # 防止输入空消息
        if not msg:
            continue
        tcp_client_socket.send(msg.encode('utf-8'))  # 收发消息一定要二进制，记得编码
        if msg == '0':
            tcp_client_socket.close()
            break

        recv_data = tcp_client_socket.recv(1024).decode('utf-8')
        print(f'{tt.now(1)}s收到返回数据: "{recv_data}"')

        if recv_data is None:
            break

        tt.sleep(2)

    tcp_client_socket.close()


if __name__ == '__main__':
    main()